package com.action.app_qrcode;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import net.sf.json.JSONObject;

import com.enetiy.Signup;
import com.enetiy.Users;
import com.operation.HttpSendUrl;
import com.service.FilterService;
import com.service.LoginService;
import com.util.DataUtil;
import com.util.IPUtil;
import com.util.Log4gUtil;
import com.util.Md5Util;

public class QRManageAction {
	private FilterService filterService;
	private LoginService loginService;
	
	private String params;
	
	private int userid;
	private String sessionid;
	private String  naming;
	private String sign;
	
	/**
	 * 
	 * QR用户认证  >Nf044
	 */
		public void userQRVerify(){
			if(params!=null&&!params.equals("")){
				org.json.JSONObject dataParser = DataUtil.dataParser(params);
				if(dataParser.has("userid"))
					userid = dataParser.getInt("userid");
				if(dataParser.has("naming"))
					 naming =  dataParser.getString("naming");
				if(dataParser.has("sessionid"))
					sessionid =  dataParser.getString("sessionid");
				if(dataParser.has("pequest_sign"))
					sign =  dataParser.getString("sign");
			}
			
			JSONObject sendJson = DataUtil.sendJson();
			Log4gUtil.getLog().info("userQRVerify-userid:"+userid+"-进入二维码 用户认证-数据:"+params);
			HttpServletResponse response = ServletActionContext.getResponse();
			PrintWriter out =null;
			try {
				out = response.getWriter();
				Signup signUp = filterService.selectSignUp(naming);
				if(signUp!=null&&sign!=null){
					String submit = "userid="+userid
							+"&sessionid="+sessionid
							+"&naming="+ naming
							+"&secretKey="+signUp.getSecretKey();
					String md5 = Md5Util.getMd5(submit);
					
					Users user = loginService.selectUserId(userid);
					if(sign.equals(md5)&&user!=null){
						String headimg = IPUtil.getIp()+"/img/uersImg/headImg/"+user.getHeadImg();
						String param = "userid="+userid
								+"&nickname="+user.getNickname()
								+"&headimg="+headimg
								+"&niuNum="+user.getNiuNum()
								+"&sessionid="+sessionid;
						
						HttpSendUrl send = new HttpSendUrl();
						String data = send.getSendRequest(signUp.getGoUrl(), param, "POST",5000);
						org.json.JSONObject  js = new org.json.JSONObject (data);
						if(js.getInt("code")==1){
							sendJson.put("code", 1);
						}else{
							sendJson.put("code", 0);
							sendJson.put("reason",3);
							sendJson.put("msg","未找到缓存");
						}
					}else{
						sendJson.put("code", 0);
						sendJson.put("reason",1);
						sendJson.put("msg","用户信息不存在或密钥不对");
					}
				}else{
					sendJson.put("code", 0);
					sendJson.put("reason",2);
					sendJson.put("msg","naming所在规则不存在或签名为空");
				}
			} catch (Exception e) {
				sendJson.put("code",-1);
				sendJson.put("msg","服务器异常");
				e.printStackTrace();
			}
			out.print(sendJson);
		}
	
	public void setParams(String params) {
			this.params = params;
		}
	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}
	public void setUserid(int userid) {
		this.userid = userid;
	}

	public void setSessionid(String sessionid) {
		this.sessionid = sessionid;
	}


	public void setNaming(String naming) {
		this.naming = naming;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}

	public void setFilterService(FilterService filterService) {
		this.filterService = filterService;
	}
}
